define(["jquery",
        "domReady",
        "editable"],
        function($){
    "use strict";
    
    // This file in combination with the rendered questions replaces the javascript encoded question and the activity-generic.js
    // file.
    // It is loaded using requireJS and defines its own dependendencies.
    /*
     * Generic multiple choice activation for both group and standard multiple choice.
     * These 2 click functions bind to all multiple choice questions in the markup and control their behavior
     * All the markup is already in the Dom, these are just controlling the behavior.
     */
    // bind to multiple choices and clear the feedback box when the multiple choice is clicked
    $(".gcb_q_multiple_choice_container .q_multiple_choice").click(function(){
    	var container = $(this).parents(".gcb_q_multiple_choice_container");
    	$(container).find(".gcb_q_feedback_box").html("");
    	$(container).find(".gcb_q_multiple_choice").each(function(){
    		$(this).removeClass("gcb_q_correct_selected");
    		$(this).removeClass("gcb_q_incorrect_selected");
    	});
      });

    // bind to check answer, when clicked find the container, then find what was checked
    // then update the feedback box with feedback from the tag preceding the item checked.
    // and if the answer was correct, add a class to the selected answer
      $(".gcb_q_multiple_choice_container .gcb_q_check_answer").click(function(){
          var container = $(this).parents(".gcb_q_multiple_choice_container");
          var feedback_box = $(container).find(".gcb_q_feedback_box");
		  $(feedback_box).html("");
		  var ncorrect = 0;
		  var nincorrect = 0;
		  var nquestions = 0;
		  var nchecked = 0;
		  $(container).find('.gcb_q_choices').each(function() { nquestions++ });
		  // find all the checked answers and collect the feedback.
          $(container).find(".q_multiple_choice:checked").each(function(){
        	  $(feedback_box).append('<br>');
    		  $(feedback_box).append($(this).prev().html());
    	      if ( $(this).hasClass("gcb_q_correct") ) {
        	      $(this).addClass("gcb_q_correct_selected");
    	          ncorrect++;
    	      } else {
        	      $(this).addClass("gcb_q_incorrect_selected");
    	    	  nincorrect++;
    	      }
    	      nchecked++;
          });

      	
	      if ( nquestions > 1 ) {
	    	if ( nchecked == nquestions && ncorrect === nquestions ) {
	      	  $(container).find(".gcb_q_multiple_choice_question:checked").each(function(){
	  	          $(this).addClass("gcb_q_correct_selected");
	      		});
		      // the element before the feedback box contains the message if all were correct.
		      $(feedback_box).append("<br>");
		      $(feedback_box).append(
		    			  $(feedback_box).prev().
		    			  find('.gcb_q_all_correct').html()
		    			  );
	        } else {
	      	  // the element before the feedback box contains the message if all were correct.
			  $(feedback_box).append("<br>");
	      	  $(feedback_box).append(
	      			  $(feedback_box).prev().find('.gcb_q_some_correct').html().
	      			  		replace("__CORRECT__",ncorrect).
	      			  		replace("__TOTAL__",nquestions));	
	        }
          }
      });


	  /**
	   * Free text answer box
	   */
	      		
	  // if the user clicks on the prompt show the prompt
	  $('.gcb_q_free_text_container .gcb_q_show_answer_prompt').click(function(){
		  var container = $(this).parents(".gcb_q_free_text_container");
		  $(container).find('.gcb_q_answer_prompt').show();
	  });
	  $('.gcb_q_free_text_container .gcb_q_check_answer').click(function(){
		  var container = $(this).parents(".gcb_q_free_text_container");
  	      var textValue = $(container).find('.gcb_q_answer').val();
          var feedback_box = $(container).find(".gcb_q_feedback_box");
		  $(feedback_box).html("");
  	      textValue = textValue.replace(/^\s+/,''); //trim leading spaces
  	      textValue = textValue.replace(/\s+$/,''); //trim trailing spaces
  	      var correct = 0;
  	      var incorrect = 0;

  	      // iterate through all checkanswer elemets looking for a match
  	      $(container).find('.gcb_q_checkanswers').each(function(){
  	    	  var pattern = $(this).attr("regex");
  	    	  var flags = $(this).attr("regex_flags");
  	    	  if ( !flags ) {
  	    		  flags = "";
  	    	  }
  	    	  var re = new RegExp(pattern, flags);
  	    	  if ( re.test(textValue) ) {
  	    		  correct++;
  	    		  $(feedback_box).append("<br>");
  	    		  $(feedback_box).append($(this).find(".gcb_q_correct_answer").html());
  	    		  $(feedback_box).show();
  	    	  } else {
  	    		  incorrect++;
  	    		  $(feedback_box).append("<br>");
  	    		  $(feedback_box).append($(this).find(".gcb_q_incorrect_answer").html());
  	    		  $(feedback_box).show();
  	    	  }  	    	  
  	      });
  	    });

	
});